package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 角色表服务实现类
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Override
    public PageResponse<RoleVo> findRolePage(RoleDto roleDto, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<List<Role>> page = roleMapper.selectPage(roleDto);
        PageResponse<RoleVo> pageResponse = PageResponse.of(page, RoleVo.class);
        return pageResponse;
    }

    @Override
    public void save(RoleDto dto) {
        Role bean = BeanUtil.toBean(dto, Role.class);
        roleMapper.insert(bean);
    }

    @Override
    public RoleVo insertById(Long id) {
        Role role = roleMapper.selectByPrimaryKey(id);
        return BeanUtil.toBean(role, RoleVo.class);
    }

    @Override
    public Boolean updateRole(RoleDto roleDto) {

        //TODO 转换RoleVo为Role
        Role role = BeanUtil.toBean(roleDto, Role.class);
//        TODO 该角色已分配用户，不能禁用

//        TODO 修改角色
        roleMapper.updateByPrimaryKeySelective(role);
//        TODO 判断是否修改角色对应的资源数据
        if (ObjectUtils.isEmpty(roleDto.getCheckedResourceNos())) {
            return true;
        }
//        TODO 删除原有角色资源中间信息
        roleResourceMapper.deleteByRoleId(roleDto.getId());
//        TODO 保存原有角色资源中间信息
        List<RoleResource> roleResources=new ArrayList<>();
        for (String resourceNo : roleDto.getCheckedResourceNos()) {
            RoleResource roleResource=new RoleResource();
            roleResource.setRoleId(roleDto.getId());
            roleResource.setResourceNo(resourceNo);
            roleResource.setDataState("0");
            roleResources.add(roleResource);
        }
//        TODO 如果集合为空，则结束请求
        if (ObjectUtils.isEmpty(roleResources)) {
            return true;
        }
//        TODO 批量保存角色和资源中间信息的关系数据
        roleResourceMapper.batchInsert(roleResources);
        return true;
    }

    @Override
    public Set<String> findCheckedResources(Long id) {
        return roleResourceMapper.selectResourceNoByRoleId(id);
    }

    @Override
    public void delete(Long id) {
        roleResourceMapper.deleteByRoleId(id);
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<RoleVo> list(RoleDto roleDto) {
        Role bean = BeanUtil.toBean(roleDto, Role.class);
        List<Role> role = roleMapper.list(bean);
        List<RoleVo> roleVos = BeanUtil.copyToList(role, RoleVo.class);
        return roleVos;
    }

}